import 'package:flutter/material.dart';
import 'package:mirror/AppStyle.dart';
import 'package:mirror/api/MessageApi.dart';
import 'package:mirror/bean/GroupBean.dart';
import 'package:mirror/page/home/pages/message/ChatPage.dart';
import 'package:mirror/widget/EmptyWidget.dart';

class GroupListPage extends StatefulWidget {
  const GroupListPage({super.key});

  @override
  State<GroupListPage> createState() => _GroupListPageState();
}

class _GroupListPageState extends State<GroupListPage> {
  List<GroupBean> list = [];

  @override
  void initState() {
    super.initState();
    refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          "我加入的群聊(${list.length})",
          style: const TextStyle(fontSize: 16),
        ),
      ),
      body: SafeArea(
        child: Column(
          children: [
            Expanded(
                child: list.isEmpty
                    ? const EmptyWidget()
                    : Padding(
                        padding: const EdgeInsets.all(10),
                        child: ListView.builder(
                            itemCount: list.length,
                            itemBuilder: (c, i) {
                              return Padding(
                                padding: const EdgeInsets.symmetric(
                                    horizontal: 5, vertical: 3),
                                child: InkWell(
                                  onTap: () {
                                    Navigator.pushReplacement(
                                        context,
                                        MaterialPageRoute(
                                            builder: (c) => ChatPage(
                                                roomId: "${list[i].roomId}",
                                                title: "${list[i].roomName}")));
                                  },
                                  child: Container(
                                    padding: const EdgeInsets.symmetric(
                                        horizontal: 10, vertical: 5),
                                    decoration: BoxDecoration(
                                        borderRadius: Constant.borderRadius,
                                        boxShadow: Constant.boxShadow,
                                        color: Colors.white),
                                    child: Row(
                                      children: [
                                        Container(
                                          padding: const EdgeInsets.all(15),
                                          decoration: const BoxDecoration(
                                              shape: BoxShape.circle,
                                              color: Colors.blue),
                                          child: Text(
                                            "${list[i].roomName}"
                                                .substring(0, 1),
                                            style: const TextStyle(
                                                color: Colors.white,
                                                fontSize: 18),
                                          ),
                                        ),
                                        const SizedBox(
                                          width: 10,
                                        ),
                                        Text("${list[i].roomName}")
                                      ],
                                    ),
                                  ),
                                ),
                              );
                            }),
                      ))
          ],
        ),
      ),
    );
  }

  Future<void> refresh() async {
    try {
      // CommonLoading.show();
      var l = await MessageApi.getAllOfMyGroups();
      if (l != null) {
        list.addAll(l);
      }
      setState(() {});
      // CommonLoading.hide();
    } catch (e) {
      // CommonLoading.hide();
      print(e);
    }
  }
}
